package com.fumin.controller;

import com.fumin.common.JsonData;
import com.fumin.dto.DeptLevelDto;
import com.fumin.param.DeptParam;
import com.fumin.service.SysDeptService;
import com.fumin.service.SysTreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 添加部门员工的controller
 *
 * @Author xiayuxuanmin
 * @Date 2019/10/22
 * @Descriptionsave.jso
 */
@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {
    @Resource
    private SysDeptService sysDeptService;
    @Resource
    private SysTreeService sysTreeService;

    //项目默认以JSON返回的，用.json结尾，返回页面的以.page结尾
    @RequestMapping("/dept.page")
    public ModelAndView page() {
        //在视图解析器中，根据prefix和suffix查找对应的JSP文件，这里是查找/WEB-INF下面的/views/dept.jsp文件
        return new ModelAndView("dept");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveDept(DeptParam param) {
        sysDeptService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/tree.json")
    @ResponseBody
    public JsonData tree() {
        List<DeptLevelDto> dtoList = sysTreeService.deptTree();
        return JsonData.success(dtoList);
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateDept(DeptParam param) {
        sysDeptService.update(param);
        return JsonData.success();
    }

    @RequestMapping("/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("id") int id) {
        sysDeptService.delete(id);
        return JsonData.success();
    }
}
